<?php
defined('_VISIT_URL_') or exit('No direct script access allowed');

class customer {

	//获取客服列表
	public function getContactList() {
		$type = getPost('type0');
		$isuse = getPost('isuse');
		$keyword = getPost('keyword');

		$queryitem = '';
		if ($type != '')
			$queryitem .= ' and type = ' . $type;
		if ($isuse != '')
			$queryitem .= ' and isuse = ' . $isuse;
		if ($keyword != '')
			$queryitem .= " and name like '%" . $keyword . "%'";

		$rst = new sqlcon();

		//获取客服总数
		$info = $rst->getInfo('count(*) as num', 'kfer', 'id != 0' . $queryitem);

		//获取客服列表
	    $list = $rst->getList('id, kferid, name, introduce, pic, phone, type, isuse, sx', 'kfer', 'id != 0' . $queryitem, 'id asc');
	    for ($i = 0; $i < count($list); $i++) {
	    	//格式化头像路径
        	if (!empty($list[$i]['pic'])) {
                $list[$i]['pic'] = _HTTP_PATH_ . $list[$i]['pic'];
            }
            else {
                $list[$i]['pic'] = 'static/pic/pc/logo_default.jpg';
            }
	    }

	    $result = util::getSuccess('success');
	    $result['listCount'] = $info['num'];
	    $result['contactList'] = $list;
		return $result;
	}

	//保存客服信息
	public function saveContact() {
		if (getPost('flag') != 'add' && getPost('flag') != 'edit') {
			return util::getValidError('保存客服操作异常');
		}

		if (empty(getPost('name')) || valid::isWicked(getPost('name'))) {
			return util::getValidError('姓名格式错误');
		}

		if (empty(getPost('introduce')) || valid::isWicked(getPost('introduce'))) {
			return util::getValidError('简介格式错误');
		}

		if (empty(getPost('phone')) || (!valid::isMobile(getPost('phone')) && !valid::isTelphone(getPost('phone')))) {
			return util::getValidError('联系电话格式异常');
		}

		if (!valid::isNum(getPost('type0'))) {
			return util::getValidError('类型格式异常');
		}

		if (getPost('isuse') != '1' && getPost('isuse') != '0') {
			return util::getValidError('状态格式错误');
		}

		if (!valid::isNum(getPost('sx'))) {
			return util::getValidError('顺序格式异常');
		}

		$rst = new sqlcon();
		$uuid = '';

		if (getPost('flag') == 'add') {
			$uuid = util::getUUID();

			//保存头像
	        $headPath = '';
	        if (strpos(getPost('headpic'), ';base64,')) {
	            $headPath = 'upload/contact/' . util::getFileName(getPost('headpic'));
	            util::saveFile(getPost('headpic'), _IMGS_PATH_ . $headPath);
	        }

	        //保存二维码
	        $qrcodePath = '';
	        if (strpos(getPost('qrcodepic'), ';base64,')) {
	            $qrcodePath = 'upload/contact/' . util::getFileName(getPost('qrcodepic'));
	            util::saveFile(getPost('qrcodepic'), _IMGS_PATH_ . $qrcodePath);
	        }

	        //保存信息
	        $rst->addCollect('kferid', $uuid);
			$rst->addCollect('name', getPost('name'));
			$rst->addCollect('introduce', getPost('introduce'));
			$rst->addCollect('pic', $headPath);
			$rst->addCollect('phone', getPost('phone'));
			$rst->addCollect('qrcode', $qrcodePath);
			$rst->addCollect('type', getPost('type0'));
			$rst->addCollect('isuse', getPost('isuse'));
			$rst->addCollect('sx', getPost('sx'));
			$rst->insertInfo('kfer');
		}
		else {
			if (empty(getPost('kferid'))) {
				return util::getValidError('ID格式错误');
			}

			$uuid = getPost('kferid');

			//保存头像
	        $headPath = '';
	        if (strpos(getPost('headpic'), ';base64,')) {
	            $headPath = 'upload/contact/' . util::getFileName(getPost('headpic'));
	            util::saveFile(getPost('headpic'), _IMGS_PATH_ . $headPath);

	            //删除原先的头像
	            $info = $rst->getInfo('pic', 'kfer', "kferid = '" . getPost('kferid') . "'");
	            if (!empty($info['pic'])) {
	            	$file_path = _IMGS_PATH_ . $info['pic'];
					if (file_exists($file_path)) {
						unlink($file_path);
					}
	            }
	        }
	        else {
	            $headPath = str_replace(_HTTP_PATH_, '', getPost('headpic'));
	        }

	        //保存二维码
	        $qrcodePath = '';
	        if (strpos(getPost('qrcodepic'), ';base64,')) {
	            $qrcodePath = 'upload/contact/' . util::getFileName(getPost('qrcodepic'));
	            util::saveFile(getPost('qrcodepic'), _IMGS_PATH_ . $qrcodePath);

	            //删除原先的二维码
	            $info = $rst->getInfo('qrcode', 'kfer', "kferid = '" . getPost('kferid') . "'");
	            if (!empty($info['qrcode'])) {
	            	$file_path = _IMGS_PATH_ . $info['qrcode'];
					if (file_exists($file_path)) {
						unlink($file_path);
					}
	            }
	        }
	        else {
	            $qrcodePath = str_replace(_HTTP_PATH_, '', getPost('qrcodepic'));
	        }

	        //保存信息
			$rst->addCollect('name', getPost('name'));
			$rst->addCollect('introduce', getPost('introduce'));
			$rst->addCollect('pic', $headPath);
			$rst->addCollect('phone', getPost('phone'));
			$rst->addCollect('qrcode', $qrcodePath);
			$rst->addCollect('type', getPost('type0'));
			$rst->addCollect('isuse', getPost('isuse'));
			$rst->addCollect('sx', getPost('sx'));
			$rst->addCollect('kferid', $uuid);
			$rst->updateInfo('kfer');
		}

		$result = util::getSuccess('success');
	    $result['kferid'] = $uuid;
		return $result;
	}

	//获取客服信息
	public function getContactInfo() {
		if (empty(getPost('kferid'))) {
			return util::getValidError('ID格式错误');
		}

		$rst = new sqlcon();

		$info = $rst->getInfo('*', 'kfer', "kferid = '" . getPost('kferid') . "'");
		if (empty($info)) {
			return util::getDataError('信息不存在');
		}

		//格式化头像路径
		if (!empty($info['pic'])) {
			$info['pic'] = _HTTP_PATH_ . $info['pic'];
		}

		//格式化二维码路径
		if (!empty($info['qrcode'])) {
			$info['qrcode'] = _HTTP_PATH_ . $info['qrcode'];
		}

		$result = util::getSuccess('success');
		$result['contactInfo'] = $info;
		return $result;
	}

	//删除客服信息
	public function deleteContact() {
		if (empty(getPost('kferid'))) {
			return util::getValidError('ID格式错误');
		}

		$rst = new sqlcon();
		$info = $rst->getInfo('pic, qrcode', 'kfer', "kferid = '" . getPost('kferid') . "'");
		if (empty($info)) {
			return util::getDataError('信息不存在');
		}

		//删除头像
		if (!empty($info['pic'])) {
        	$file_path = _IMGS_PATH_ . $info['pic'];
			if (file_exists($file_path)) {
				unlink($file_path);
			}
        }

        //删除二维码
		if (!empty($info['qrcode'])) {
        	$file_path = _IMGS_PATH_ . $info['qrcode'];
			if (file_exists($file_path)) {
				unlink($file_path);
			}
        }

        //删降客服信息
		$rst->addCollect('kferid', getPost('kferid'));
		$rst->deleteInfo('kfer');

		return util::getSuccess('success');
	}
	
}